package com.ruoyi.client.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.client.pojo.form.NovelInsertForm;
import com.ruoyi.client.pojo.vo.NovelRewardsVo;
import com.ruoyi.client.pojo.vo.PageListVo;
import com.ruoyi.client.service.NovelRewardsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Anonymous  // 允许匿名访问
@RestController
@RequestMapping("/novelrewards")
public class NovelRewardsController extends BaseController {
    @Resource
    private NovelRewardsService novelRewardsService;

    /*
    * 增加评论*/
    @PostMapping("/insert")
    public R<Long> insert(@Valid @RequestBody NovelInsertForm form) {
        //保存并返回
        Long RewardId = novelRewardsService.insert(form);
        return RewardId > 0 ? R.ok(RewardId) : R.fail();
    }

    /*更改评论*/
    @PutMapping("/update")
    public R<String> update(@RequestBody @Valid NovelInsertForm form) {
        return novelRewardsService.update(form) ? R.ok() : R.fail();
    }

    /*查询所有评论*/
    @GetMapping("/list")
    public R<PageListVo<NovelRewardsVo>> listAll() {
        startPage();//设置分页，调用的父方法
        List<NovelRewardsVo> list = novelRewardsService.selectAll();

        PageInfo pageInfo = new PageInfo(list);
        PageListVo<NovelRewardsVo> pageListVo = new PageListVo<>();

        pageListVo.setTotalCount(pageInfo.getTotal());
        pageListVo.setTotalPage(pageInfo.getPages());
        pageListVo.setList(list);

        return R.ok(pageListVo);
    }

    /*查询评论详情By评论id*/
    @GetMapping("/{rewardId}")
    public R<NovelRewardsVo> selectNovelById(@PathVariable("rewardId") Long rewardId) {
        NovelRewardsVo novelRewardsVo = novelRewardsService.selectNovelById(rewardId);
        return R.ok(novelRewardsVo);
    }

    /*查询评论*/
    @GetMapping("/selectVoMyList")  //Ruoyi框架分页查询，使用GET。为什么？自行分析框架源码
    public R<PageListVo<NovelRewardsVo>> list(NovelInsertForm form)  // 移除@Valid，因为查询时novelId可以为空（可以通过chapterId查询）
    {
        startPage();//设置分页，调用的父方法
        List<NovelRewardsVo> list = novelRewardsService.selectVoMyList(form);

        PageInfo pageInfo = new PageInfo(list);
        PageListVo<NovelRewardsVo> pageListVo = new PageListVo<>();

        pageListVo.setTotalCount(pageInfo.getTotal());
        pageListVo.setTotalPage(pageInfo.getPages());
        pageListVo.setList(list);

        return R.ok(pageListVo);  // 接口返回
    }

    /*删除评论*/
    @DeleteMapping("/{rewardId}")
    public R deleteById(@PathVariable("rewardId") Long rewardId) {
        return novelRewardsService.deleteById(rewardId) ? R.ok() : R.fail();
    }

    /*点赞评论*/
    @PostMapping("/like/{rewardId}")
    public R likeComment(@PathVariable("rewardId") Long rewardId) {
        return novelRewardsService.likeComment(rewardId) ? R.ok("点赞成功") : R.fail("点赞失败");
    }

    /*取消点赞评论*/
    @PostMapping("/unlike/{rewardId}")
    public R unlikeComment(@PathVariable("rewardId") Long rewardId) {
        return novelRewardsService.unlikeComment(rewardId) ? R.ok("已取消点赞") : R.fail("取消失败");
    }

}